package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.McgsAlarmInfoBo;
import com.ruoyi.tb.lmt.domain.vo.McgsAlarmInfoVo;
import com.ruoyi.tb.lmt.domain.McgsAlarmInfo;
import com.ruoyi.tb.lmt.mapper.McgsAlarmInfoMapper;
import com.ruoyi.tb.lmt.service.IMcgsAlarmInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 制动阀试验台_MCGS_AlarmInfoService业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class McgsAlarmInfoServiceImpl implements IMcgsAlarmInfoService {

    private final McgsAlarmInfoMapper baseMapper;

    /**
     * 查询制动阀试验台_MCGS_AlarmInfo
     */
    @Override
    public McgsAlarmInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询制动阀试验台_MCGS_AlarmInfo列表
     */
    @Override
    public TableDataInfo<McgsAlarmInfoVo> queryPageList(McgsAlarmInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<McgsAlarmInfo> lqw = buildQueryWrapper(bo);
        Page<McgsAlarmInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询制动阀试验台_MCGS_AlarmInfo列表
     */
    @Override
    public List<McgsAlarmInfoVo> queryList(McgsAlarmInfoBo bo) {
        LambdaQueryWrapper<McgsAlarmInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<McgsAlarmInfo> buildQueryWrapper(McgsAlarmInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<McgsAlarmInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, McgsAlarmInfo::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDjNo()), McgsAlarmInfo::getDjNo, bo.getDjNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsAlarmId()), McgsAlarmInfo::getMcgsAlarmId, bo.getMcgsAlarmId());
        lqw.like(StringUtils.isNotBlank(bo.getMcgsAlmName()), McgsAlarmInfo::getMcgsAlmName, bo.getMcgsAlmName());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeS()), McgsAlarmInfo::getMcgsTimeS, bo.getMcgsTimeS());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeSms()), McgsAlarmInfo::getMcgsTimeSms, bo.getMcgsTimeSms());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeE()), McgsAlarmInfo::getMcgsTimeE, bo.getMcgsTimeE());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeEms()), McgsAlarmInfo::getMcgsTimeEms, bo.getMcgsTimeEms());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeA()), McgsAlarmInfo::getMcgsTimeA, bo.getMcgsTimeA());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeAms()), McgsAlarmInfo::getMcgsTimeAms, bo.getMcgsTimeAms());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsId()), McgsAlarmInfo::getMcgsId, bo.getMcgsId());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsType()), McgsAlarmInfo::getMcgsType, bo.getMcgsType());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsLimitValue()), McgsAlarmInfo::getMcgsLimitValue, bo.getMcgsLimitValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsStandValue()), McgsAlarmInfo::getMcgsStandValue, bo.getMcgsStandValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsInfo()), McgsAlarmInfo::getMcgsInfo, bo.getMcgsInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), McgsAlarmInfo::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), McgsAlarmInfo::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, McgsAlarmInfo::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), McgsAlarmInfo::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, McgsAlarmInfo::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增制动阀试验台_MCGS_AlarmInfo
     */
    @Override
    public Boolean insertByBo(McgsAlarmInfoBo bo) {
        McgsAlarmInfo add = BeanUtil.toBean(bo, McgsAlarmInfo.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改制动阀试验台_MCGS_AlarmInfo
     */
    @Override
    public Boolean updateByBo(McgsAlarmInfoBo bo) {
        McgsAlarmInfo update = BeanUtil.toBean(bo, McgsAlarmInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(McgsAlarmInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除制动阀试验台_MCGS_AlarmInfo
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
